﻿/*******************************************************************************

  NLocalizer (C) 2010-2012 Chris Prusik (KAP1 Ltd www.kap1.net)
  The fast, simple solution for localizing .NET applications, by text files.
  Latest version: http://NLocalizer.codeplex.com/

  $Id$

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

*******************************************************************************/
using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.IO;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NLocalizer.Tests
{
    [TestClass]
    public class RuntimeCompilerTest
    {
        [TestMethod]
        public void ToStringTest()
        {
            List<string> list = new List<string>();
            Assert.AreEqual("", RuntimeCompiler.ToString(list));
            list.Add("pierwszy  ");
            Assert.AreEqual("pierwszy", RuntimeCompiler.ToString(list));
            list.Add("drugi  ");
            Assert.AreEqual("pierwszy, drugi", RuntimeCompiler.ToString(list));
        }

        [TestMethod]
        public void CodeTemplatesTest()
        {
            Assert.AreEqual(11, RuntimeCompiler.CodeTemplates.Count);
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("Code"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("Using"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("RememberClass"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("RestoreClass"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("TranslateClass"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("StaticClass"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("StaticClassRemember"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("StaticClassTranslate"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("DynamicClass"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("DynamicClassRemember"));
            Assert.IsTrue(RuntimeCompiler.CodeTemplates.ContainsKey("DynamicClassTranslate"));

            Assert.AreEqual("            obj.$(PropertyName) = translation.GetText(language, \"$(ClassName)\", \"$(PropertyName)\");\r\n",
                RuntimeCompiler.GetTemplate("DynamicClassTranslate"));

        }

        [TestMethod]
        public void ChangeRuntimeCodeTemplatesFileTest()
        {
            Assert.AreEqual(11, RuntimeCompiler.CodeTemplates.Count);
            RuntimeCompiler.CodeTemplatesAsText = "@DynamicClassTranslate\r\nobj.$(PropertyName)  =  translation.GetText(language, \"$(ClassName)\", \"$(PropertyName)\");";
            Assert.AreEqual(11, RuntimeCompiler.CodeTemplates.Count);
            Assert.IsTrue(RuntimeCompiler.CodeTemplatesAsText.Length > 
                "@DynamicClassTranslate\r\nobj.$(PropertyName)  =  translation.GetText(language, \"$(ClassName)\", \"$(PropertyName)\");".Length);
            Assert.AreEqual("obj.$(PropertyName)  =  translation.GetText(language, \"$(ClassName)\", \"$(PropertyName)\");",
                RuntimeCompiler.CodeTemplates["DynamicClassTranslate"].Trim());
        }

        [TestMethod]
        public void CodeTest()
        {
            Translation translation = new Translation();
            translation.Read(@"..\..\..\NLocalizer.Tests");
            Assembly assembly;
            string dir = Directory.GetCurrentDirectory();
            try
            {
                string exe = @"..\..\..\Bin\NLocalizer.Example.exe";
                Directory.SetCurrentDirectory(Path.GetDirectoryName(Path.GetFullPath(exe)));
                assembly = AppDomain.CurrentDomain.Load(Path.GetFullPath(exe));
            }
            finally
            {
                Directory.SetCurrentDirectory(dir);
            }
            //Assembly assembly = Assembly.LoadFile(Path.GetFullPath(@"..\..\..\Bin\NLocalizer.Example.exe"));
            StringBuilder code = translation.GenerateCode(assembly);
            File.WriteAllText(@"..\..\..\NLocalizer.Tests\RuntimeTranslator.cs", code.ToString());
        }

        [TestMethod]
        public void CompileTest()
        {
            Translation translation = new Translation();
            translation.Read(@"..\..\..\NLocalizer.Tests\Bin\Debug");
            CompilerResults compiler = translation.Compile(@"..\..\..\NLocalizer.Tests\Bin\Debug");
            Assert.IsNotNull(compiler);
            Assert.IsNotNull(compiler.CompiledAssembly);
            Assert.AreEqual(0, compiler.Errors.Count);
        }

        [TestMethod]
        public void ShowTestFormTest()
        {
            Translator.Translation.Read(@"..\..\..\NLocalizer.Tests\Bin\Debug");
            object form = Translator.Create("TestForm");
            Assert.IsNotNull(form);
            Assert.IsTrue(form is Form);
        }

    }
}
